package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id="2021000121664287";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCRxE4lsyChOR5fExqRe48PXsX+K/m56/vvhrsOiw6VErCaUkIQD5N640d+87RMfvhLnucesRkyrvjvvARV0XiK5ddDSPnWPWvzU/jM6qVCPeoTT9wFX8T5Tvpr2n/xS29MsZ8cPiGubmkfKrvm7MJCYU7xyXUrG71xpmmr7wzXMiCDS8HRwXB37Gs0u1RDx/X+9sKc44xARkHiqYskXeUs26/bD8ZfaI+K6vB2FWdNdkxvygaWAoGdpThrMnlKih8/VDuBrQMNamZ6rc98hQ9GiTqzbLCcKJ3HBfZAkmOPbfdd0qttVqkidvZUoKtkKiaprgM0noqFthrPT794+9AvAgMBAAECggEAM2T2dL5im9WqyrTOokrhVvcAciyJIS9+JaboXylSW+c6Ez87gbQsOKDX/X/pqyr9WpRNQvoIO2GL7eLp2GRBzx9YtaWOSSZg4GtUBtQYrnFsDaETn2Vz6r8Y9lwUzjvdoznhMrEJaRoOwGlt1O7XU1ZB4mg4gpmGQwL8sR1Y9qpyntMFSB7VAEJk9auCR8Lug0j5Zg8NKBDnHs6nAKXJIox9gTLpV8fE+Ug5s33s/DjlVl7BP5FMF7Ojgb3CdXQoft8eATaWNl+mfkdk5yY93MWLPKOflBmRVwU+PL+T36hfJrhDlgl+qE+rYqmAeDpR1TdAKYC01V7/IyzKVrkuiQKBgQDGESzCSd8XAEQJMbSu83teiySNgwu4fLpnwhryTBGKdcQQrXUf/WU3CQEPOrAIXR0aRMWx1bOCS10430iq7xUJY0qRT2gPVUYK2JkZHM4DYF3eXTs76iCtW45Lzc83QsSVF3I8LT7gcLsrjE26EOvpGvSXqSeabry4e/10+s4L3QKBgQC8ZwB5ggLXsDBftOohFYX3Vn1pv1tUzzvfF/7ckR5j84gjcQDkG1bteUUU2gnudAW7gpg0jWf2PJpSu9ZwHil+od3ku8vSX01I6/H0tXP2saairLAKdoZPq2tgX/mCFKqMng/BMpY7Ey4NV0EJqExSMQpNi743OflpZFQO081BewKBgQCJu0W5sW7d6sRk9Fil1BC1FOMNONR8rNde8Z6VYQpNk0kNI+GvpiCtHajL806X8lAbhTlIScezUj74HmTE4M5rIc2nsGU3TGARiL8ltF8Z//gmegSHzG5+voz775kp31ceKhi4+emgXyP4y279dWjnR2RQ1kMA5AJ981Kz9vgzTQKBgCBwS7CgJgUlHO8do0JXBl3z0lwmyn7rv49F/mfTxLvI7qPLjS+5VVpGCxLwB826xFHoxnQl9MR84sVj+iQLzXE0RZRgkVODfTcVCO/tXi4XoG1Z9mWWysFk7nQFklxHWqYjgOru3tcFEmTRpS0YgSuVrgR0DyhLHxp6X869wRWJAoGBAJkEQ60ObQsEGB7mT6V7YBRBVtdMQ01bkZtBT6sWgg+F45Tdh/zvS9JJd4FkXLGnRWGz4kOuM4QNiwH+SNe+k6FYeM5FYVkAcYP/6GSzpFans2YuAvjeueYZl/pngEF4RvBWaDYOX3p79A5ilLgyDt2ZD9rR7yKG4IwELHg9Ce1z";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtz40GyX8Jrv77CssHK5glKfIDtjeb4e2BLphm6Wju0knivjZ5wYP/kZ6TdDB2PQNwvGyjsfqhUaDOWGUbMYK5IznB1W4BHFUUdYzJ5JqmrBAZPJk9ZByWktTXSPPyi3Cld3sJqiV1xuQgp7p2O4MAgcznU92M3ww1EN+nXXfeo1GlaT0p/Dd++jjYbFaUQhLyWN5mH7D7arRiAWn8YsppbVzJiTFNO5vfnuZN1goqMRAbivyxfpoinPE2JlJq7i9B6lhrKbq/fnyG8uBcAouHOsHPEuqzTUHv5B5MPZK8KQcLzb8B1oq+kROyd24vEDrKBtlavS1QJ5QklFR9myRKwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url="http://mall-test1.sh1.k9s.run:2271/payed/notify";
    //http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    //http://工程公网访问地址/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp
    public String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type="RSA2";

    // 字符编码格式
    private  String charset="utf-8";

    //订单超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl="https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
//                + "\"total_amount\":\""+ total_amount +"\","
//                + "\"subject\":\""+ subject +"\","
//                + "\"body\":\""+ body +"\","
//                + "\"timeout_express\":\""+timeout+"\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
